package com.xcx.minservice.untilsm;

import org.springframework.web.multipart.MultipartFile;

public class VerifyFileType {
    public static String getFileTypeBySuffix(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
        suffix = suffix.toLowerCase();
        // 文件类型分类
        String[] imgTypes = new String[]{"jpg", "png","jpeg","gif","svg"};
        String[] audioTypes = new String[]{"wav", "mp3"};
        String[] videoTypes = new String[]{"avi", "mp4", "rmvb", "wmv", "flv"};
        String[] docTypes = new String[]{"pdf", "docx", "doc"};
        if (isValid(suffix, imgTypes)) {
            return "image";
        } else if (isValid(suffix, audioTypes)) {
            return "audio";
        } else if (isValid(suffix, videoTypes)) {
            return "video";
        } else if (isValid(suffix, docTypes)) {
            return "doc";
        } else {
            return null;
        }
    }

    public static boolean isValid(String contentType, String... allowTypes) {
        if (null == contentType || "".equals(contentType)) {
            return false;
        }
        for (String type : allowTypes) {
            if (contentType.equals(type)) {
                return true;
            }
        }
        return false;
    }





}
